import time
from Pages.LoginPage import LoginPage
from PublicFunc.assertion import assertion_equal
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class LoginCase:
    def __init__(self, config, logger):
        self.service = ChromeService(executable_path=ChromeDriverManager().install())
        self.driver = webdriver.Chrome(service=self.service)
        self.config = config
        self.logger = logger

    def test_login_success(self):
        case_description = '跳转到gitee页面，输入正确的用户名密码，登录成功，跳转到首页'
        login = LoginPage(self.driver)
        login.get_url(self.config.BASIC_URL)
        login.user_login()
        login.login_valid(self.config.NAME, self.config.PASSWORD)
        time.sleep(10)
        self.logger.logging("info", assertion_equal(self.driver.current_url, self.config.BASIC_URL, case_description))

    # 镜像定制查看按钮
    def test_image_build(self):
        case_description = '查看镜像定制按钮是否生效'
        login = LoginPage(self.driver)
        login.get_url(self.config.BASIC_URL)
        time.sleep(3)
        login.image_build()
        current_url = self.config.BASIC_URL + "images"
        WebDriverWait(self.driver, 10).until(EC.url_contains(current_url))
        self.logger.logging("info", assertion_equal(self.driver.current_url, self.config.BASIC_URL + "images",
                                                    case_description))

    # 用户空间页面
    def test_user_page(self):
        case_description = '查看用户空间按钮是否生效'
        login = LoginPage(self.driver)
        login.get_url(self.config.BASIC_URL)
        time.sleep(3)
        login.user_selector()
        time.sleep(3)
        login.user_page()
        time.sleep(3)
        self.logger.logging("info", assertion_equal(self.driver.current_url, self.config.BASIC_URL + "space/created",
                                                    case_description))

    # 工程构建页面
    def test_projects_build(self):
        case_description = '查看工程构建查看按钮是否生效'
        login = LoginPage(self.driver)
        login.get_url(self.config.BASIC_URL)
        time.sleep(3)
        login.projects_build()
        current_url = self.config.BASIC_URL + "projects"
        WebDriverWait(self.driver, 10).until(EC.url_contains(current_url))
        self.logger.logging("info", assertion_equal(self.driver.current_url, self.config.BASIC_URL + "projects",
                                                    case_description))

    # 用户注销
    def test_user_logout(self):
        case_description = '查看用户注销按钮是否生效'
        login = LoginPage(self.driver)
        login.get_url(self.config.BASIC_URL)
        time.sleep(3)
        login.user_selector()
        time.sleep(5)
        login.user_logout()
        WebDriverWait(self.driver, 10).until(EC.url_contains(self.config.BASIC_URL))
        self.logger.logging("info", assertion_equal(self.driver.current_url, self.config.BASIC_URL, case_description))

    # 英文切换
    def test_switch_language(self, language='en'):
        case_description = '查看切换按钮是否生效'
        login = LoginPage(self.driver)
        login.get_url(self.config.BASIC_URL)
        time.sleep(3)
        login.switch_language()
        time.sleep(4)
        if language == 'en':
            login.switch_English()
            expect_text = "Switch Language Success"
            case_description += "——英文"
        else:
            login.switch_Chinese()
            expect_text = "切换语言成功"
            case_description += "——中文"
        text = login.alert_message()
        self.logger.logging("info", assertion_equal(text, expect_text, case_description))

    def __str__(self):
        self.logger.logging("info", '*************登录模块***************')

    # 执行用例
    def run_test(self):
        self.__str__()
        self.driver.maximize_window()
        self.test_login_success()
        time.sleep(3)
        self.test_image_build()
        time.sleep(3)
        self.test_user_page()
        time.sleep(3)
        self.test_projects_build()
        time.sleep(3)
        self.test_user_logout()
        time.sleep(3)
        self.test_switch_language()
        time.sleep(3)
        self.test_switch_language("ch")
        time.sleep(3)
        self.driver.quit()

